package qf.sail.service.impl;

import com.sun.org.apache.xpath.internal.operations.Or;
import qf.sail.dao.GoodDao;
import qf.sail.dao.OrderDao;
import qf.sail.dao.OrderItemDao;
import qf.sail.dao.impl.GoodDaoImpl;
import qf.sail.dao.impl.OrderDaoImpl;
import qf.sail.dao.impl.OrderItemDaoImpl;
import qf.sail.entity.Good;
import qf.sail.entity.Order;
import qf.sail.entity.OrderItem;
import qf.sail.service.OrderService;
import qf.sail.util.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    private GoodDao goodDao= new GoodDaoImpl();

    private OrderDao orderDao = new OrderDaoImpl();

    private OrderItemDao orderItemDao = new OrderItemDaoImpl();

    public static final String IS_PAY = "已支付";

    public static final String NO_PAY = "未支付";

    /**
     * 生成订单
     * @param accountId
     * @return
     */
    @Override
    public long generateOrder(long accountId) {
        //生成一个订单信息
        Order order = new Order();

        order.setAccountId(accountId);
        order.setSn(StringUtil.generateSn());
        order.setStatus(NO_PAY);

        return orderDao.addOrder(order);
    }

    /**
     * 生成订单子项
     * @param goodId
     * @param size
     * @param color
     * @param count
     * @return
     */
    @Override
    public long addOrderItem(long goodId, long orderID, String size, String color, int count){
        OrderItem orderItem = new OrderItem();

        orderItem.setGoodId(goodId);
        orderItem.setOrderId(orderID);
        orderItem.setCount(count);
        orderItem.setSize(size);
        orderItem.setColor(color);

        return orderItemDao.addOrderItem(orderItem);
    }

    /**
     * 更新订单支付金额
     * @param orderId
     * @return
     */
    @Override
    public int updateOrderMoney(long orderId){
        List<OrderItem> items = orderItemDao.getItemsByOrderId(orderId);

        //计算订单支付金额和运费
        BigDecimal allMoney = new BigDecimal(0);
        BigDecimal totalFreight = new BigDecimal(0);

        for(OrderItem item:items){
            Good good = goodDao.getGoodById(item.getGoodId());
            allMoney = allMoney.add(good.getNewPrice().multiply(new BigDecimal(item.getCount())));
            totalFreight = totalFreight.add(good.getStartFreight().add(good.getAddFreight().multiply(new BigDecimal(item.getCount()-1))));
        }

        //更新订单信息
        Order order = new Order();

        order.setId(orderId);
        order.setNumber(items.size());
        order.setTotalFreight(totalFreight);
        order.setPayMoney(allMoney.add(totalFreight));

        return orderDao.updateOrderMoney(order);
    }

    /**
     * 根据orderId获取订单
     * @param id
     * @return
     */
    @Override
    public Order getOrderById(long id) {
        Order order = orderDao.getOrderById(id);
        List<OrderItem> orderItems = new ArrayList<>();

        //查询订单子项并加入子项列表中
        for(OrderItem item:orderItemDao.getItemsByOrderId(id)){
            Good good = goodDao.getGoodById(item.getGoodId());
            item.setGood(good);
            orderItems.add(item);
        }
        order.setOrderItemList(orderItems);
        return order;
    }

    /**
     * 更新订单地址
     * @param address
     * @param orderId
     * @return
     */
    @Override
    public int updateOrderAddress(String address, long orderId) {
        return orderDao.updateOrderAddress(address,orderId);
    }

    /**
     * 根据订单号查询订单
     * @param sn
     * @return
     */
    @Override
    public Order getOrderBySn(String sn) {
        return orderDao.getOrderBySn(sn);
    }

    /**
     * 更新订单状态
     * @param status
     * @param orderId
     * @return
     */
    @Override
    public int updateOrderStatus(String status, long orderId) {
        return orderDao.updateOrderStatus(status,orderId);
    }

    /**
     * 返回指定页数的订单列表
     * @param accountId
     * @param page
     * @return
     */
    @Override
    public List<Order> getOrderList(long accountId, int page) {
        List<Order> newOrders = new ArrayList<>();
        List<Order> oldOrders = orderDao.getOrderList(accountId,page);
        for(Order order:oldOrders){
            newOrders.add(this.getOrderById(order.getId()));
        }
        return newOrders;
    }

    /**
     * 计算订单数
     * @param accountId
     * @return
     */
    @Override
    public int getCountById(long accountId) {
        return orderDao.getCountById(accountId);
    }
}
